﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class LoginPage : WatiNBase
    {
        #region Constructors

        public LoginPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName){}

        public LoginPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Divs
        public Div LoginPanel
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Div(Find.ById("dnn_ctr_Login_DNN"));
                }
                return IEInstance.Div(Find.ById("dnn_ctr_Login_DNN"));
            }
        }
        #endregion

        #region Buttons
        //public Button LoginButton
        //{
        //    get
        //    {
        //        if (PopUpFrame != null)
        //        {
        //            return PopUpFrame.Button(Find.ById(s => s.EndsWith("ctr_Login_Login_DNN_cmdLogin")));
        //        }
        //        return IEInstance.Button(Find.ById(s => s.EndsWith("ctr_Login_Login_DNN_cmdLogin")));
        //    }
        //}
        #endregion

        #region CheckBoxes
        public CheckBox RememberMeCheckBox { get
        {
            if(PopUpFrame != null)
            {
                return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ctr_Login_chkCookie")));
            }
            return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ctr_Login_chkCookie")));
        } }
        #endregion

        #region TextFields
        public TextField ForgotPasswordUserNameField { get { return IEInstance.TextField(Find.ById(s => s.EndsWith("ctr_SendPassword_txtUsername"))); } }
        public TextField UserName
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("txtUsername")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("txtUsername")));
            }
        }
        public TextField Password
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById("dnn_ctr_Login_Login_DNN_txtPassword"));
                }
                return IEInstance.TextField(Find.ById("dnn_ctr_Login_Login_DNN_txtPassword"));
            }
        }

        #endregion

        #region Links
        public Link SendPassworkLink { get { return IEInstance.Link(Find.ByTitle("Send Password")); } }
        public Link ForgotPasswordLink { get { return IEInstance.Link(Find.ByTitle("Forgot Password ?")); } }
        public Link LoginButton
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("ctr_Login_Login_DNN_cmdLogin")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("ctr_Login_Login_DNN_cmdLogin")));
            }
        }
        #endregion

        #region Spans
        public Span LoginMessage
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.Span(Find.ById(s => s.EndsWith("ctr_ctl00_lblMessage")));
                }
                return IEInstance.Span(Find.ById(s => s.EndsWith("ctr_ctl00_lblMessage")));
            }
        }
        public Span ErrorMsg
        {
            get
            {
                if (PopUpFrame != null)
                {
                    if (IEInstance.Spans.Filter(Find.ById(s => s.EndsWith("lblMessage"))).Count > 1)
                    {
                        return PopUpFrame.Spans.Filter(Find.ById(s => s.EndsWith("lblMessage")))[2];
                    }
                    return PopUpFrame.Span(Find.ById(s => s.EndsWith("lblMessage")));
                }
                if (IEInstance.Spans.Filter(Find.ById(s => s.EndsWith("lblMessage"))).Count > 1)
                {
                    return IEInstance.Spans.Filter(Find.ById(s => s.EndsWith("lblMessage")))[2];
                }
                return IEInstance.Span(Find.ById(s => s.EndsWith("lblMessage")));
            }
        }
        #endregion

        #endregion

        #region Public Methods
        public void LoginUser(string name, string password)
        {
            IEInstance.GoTo(SiteUrl);
            System.Threading.Thread.Sleep(1500);
            LoginLink.Click();
            System.Threading.Thread.Sleep(1500);

            UserName.Value = name;
            Password.Value = password;

            LoginButton.ClickNoWait();
            System.Threading.Thread.Sleep(2500);
        }

        public void LoginUserNoGoTo(string name, string password)
        {
            LoginLink.Click();

            UserName.Value = name;
            Password.Value = password;

            LoginButton.Click();
        }
        
        public void LogoffUser()
        {
            LoginLink.ClickNoWait();
        }
        #endregion
    }
}
